﻿Imports System.IO
Imports System
Imports System.Threading
Public Class splash

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Icon = My.Resources.icon
        'Application title
        If My.Application.Info.Title <> "" Then
            ApplicationTitle.Text = My.Application.Info.Title
        Else
            'If the application title is missing, use the application name, without the extension
            ApplicationTitle.Text = System.IO.Path.GetFileNameWithoutExtension(My.Application.Info.AssemblyName)
        End If

        Version.Text = "Version: " & My.Application.Info.Version.Major & "." & My.Application.Info.Version.Minor & "." & My.Application.Info.Version.Build & " (Build " & My.Application.Info.Version.Revision & ")"


        Try
            If Not Directory.Exists(System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) & "\WSI") Then
                Shell(System.Environment.CurrentDirectory & "\WSI PreReq Installer.exe", vbNormalFocus)
                'Shell("C:\Program Files (x86)\Windows Software Installer\WSI PreReq Installer.exe", AppWinStyle.NormalFocus)
            End If
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try

        'Got There in the end (i have made the autoupdate_download form forced as top window)
        'Create New Thread
        Dim autoupdate As New Thread(New ThreadStart(AddressOf Autoupdate_check))
        autoupdate.Start()
    End Sub

    Sub Autoupdate_check()
        'This is basically the UpdateManager Start_UpdateCheck_Click script but without the onscreen updates everystep of the way
        'You may want to remove the msgbox and quit the thread instead of what it currently does
        Try
            My.Computer.Network.Ping("dev-network.com")
        Catch ex As Exception
            MsgBox("Unable to coneect to server. It may be having difficulties or your internet connection may be having problems.")
            'Thread.CurrentThread.Abort()
        End Try
        'download file
        DownloadClass.GetFile(My.Resources.Update_URL, System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) & "\WSI\Update\Auto\")

        ' Check version

        Dim line As String
        Try
            Using reader As StreamReader = New StreamReader(System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) & "\WSI\Update\Auto\version.txt")
                line = reader.ReadLine
            End Using
        Catch ex As Exception
        End Try


        Dim versionVar As String() = line.Split(",")

        'complete
        If CInt(versionVar(0)) > My.Resources.Build_Version Then
            Dim f As New AutoUpdate_Download
            f.ShowDialog()
        End If


    End Sub
End Class